package com.qipay.sp.excutor.qyhb;

import com.alibaba.fastjson.JSONObject;
import com.qipay.baas.model.Order;
import com.qipay.order.config.OrderStateEnum;
import com.qipay.order.service.OrderService;
import com.qipay.scalar.type.ApiStatusEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import com.qipay.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class QyhbNativeExcutor extends AbstractExcutor {

    private final static String API_URL = "api_url";
    @Autowired
    OrderService orderService;
    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put("orderNo", order.getTradeNo());
        apiParams.put("amount", order.getAmount());
        apiParams.put("timestamp", System.currentTimeMillis());
        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {
        Object apiUrl = getVal(spconfig, API_URL);
        Object mchId = getVal(spconfig, "mch_id");
        Object notifyUrl = getVal(spconfig, "notify_url");
        Object key = getVal(spconfig, "key");

//        params.put("agentNo", mchId);
//        params.put("notifyUrl", notifyUrl);
//        params.put("paymentType", "ALIPAY");
//        String sign = MD5Util.getSign(params, key.toString()).toLowerCase();
//        params.put("sign", sign);
//
//        String result = sendFormPost(params, apiUrl.toString());
//
//        if (StringUtils.isBlank(result)) {
//            throw new Exception("请求企业红包的支付宝WAP支付接口失败");
//        }

        Order order = orderService.getByTradeNo(params.get("orderNo").toString());
        Map<String, Object> resultMap = new HashMap<>();
//        JSONObject jsonObject = JSONObject.parseObject(result);
//        if (jsonObject.getBoolean("success") && jsonObject.getIntValue("code") == 200) {
//            resultMap.put("status", ApiStatusEnum.OK.getCode());
//            resultMap.put("redirect_url", jsonObject.getString("data"));
//            order.setState(OrderStateEnum.WAITING.getType());
//        } else {
//            resultMap.put("result_code", "1");
//            resultMap.put("message", jsonObject.getString("msg"));
//            order.setState(OrderStateEnum.REQERROR.getType());
//        }
        resultMap.put("status", ApiStatusEnum.OK.getCode());
        resultMap.put("pay_info", "http://localhost:8889/api/code_img_url/v1/conn/testApi");
        order.setState(OrderStateEnum.WAITING.getType());
        orderService.update(order);
        return resultMap;
    }

}
